package com.ksd.modules.special.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.enums.StatusEnums;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.special.entity.AdminSpecialComment;
import com.ksd.modules.special.mapper.AdminSpecialCommentMapper;
import com.ksd.modules.special.po.AdminSpecialCommentSavePO;
import com.ksd.modules.special.po.AdminSpecialCommentSearchPO;
import com.ksd.modules.special.service.IAdminSpecialCommentReplyService;
import com.ksd.modules.special.service.IAdminSpecialCommentService;
import com.ksd.modules.special.vo.AdminSpecialCommentReplyVo;
import com.ksd.modules.special.vo.AdminSpecialCommentVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminSpecialCommentServiceImpl extends BaseServiceImpl<AdminSpecialCommentMapper, AdminSpecialComment> implements IAdminSpecialCommentService {

    @Autowired
    private IAdminSpecialCommentReplyService adminSpecialCommentReplyService;

    /**
     * 查询列表
     * @param adminSpecialCommentSearchPO
     * @return
     */
    public Page<AdminSpecialCommentVo> findAdminSpecialComments(AdminSpecialCommentSearchPO adminSpecialCommentSearchPO){
        Page<AdminSpecialComment> page = new Page<>(adminSpecialCommentSearchPO.getPageNo(),adminSpecialCommentSearchPO.getPageSize());
        LambdaQueryWrapper<AdminSpecialComment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminSpecialComment::getSystemId,SystemIdThrealLocal.get().getSystemId());
        // 资源id
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialCommentSearchPO.getOpid()), AdminSpecialComment::getOpid,adminSpecialCommentSearchPO.getOpid());
        // 用户唯一标识
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminSpecialCommentSearchPO.getUuid()), AdminSpecialComment::getUuid,adminSpecialCommentSearchPO.getUuid());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialCommentSearchPO.getStatus()), AdminSpecialComment::getStatus,adminSpecialCommentSearchPO.getStatus());
        // 模糊匹配
        if(StringUtils.isNotEmpty(adminSpecialCommentSearchPO.getKeyword())){
            lambdaQueryWrapper.and(c -> c.like(
                            AdminSpecialComment::getUsername, adminSpecialCommentSearchPO.getKeyword())
                    .or().like(AdminSpecialComment::getUuid, adminSpecialCommentSearchPO.getKeyword())
                    .or().like(AdminSpecialComment::getTitle, adminSpecialCommentSearchPO.getKeyword())

            );
        }
        // 排序
        lambdaQueryWrapper.orderByDesc(AdminSpecialComment::getCreateTime);
        Page<AdminSpecialComment> adminSpecialPage = this.page(page, lambdaQueryWrapper);
        Page<AdminSpecialCommentVo> adminSpecialVoPage = tranferPageBo(adminSpecialPage, AdminSpecialCommentVo.class);
        List<AdminSpecialCommentVo> records = adminSpecialVoPage.getRecords();
        // 查询所有的第一页的子回复
        if(CollectionUtils.isNotEmpty(records)){
            for (AdminSpecialCommentVo record : records) {
                AdminSpecialCommentSearchPO adminSpecialCommentSearchPO1 = new AdminSpecialCommentSearchPO();
                adminSpecialCommentSearchPO1.setPageNo(1);
                adminSpecialCommentSearchPO1.setPageSize(3);
                adminSpecialCommentSearchPO1.setParentId(record.getId());
                Page<AdminSpecialCommentReplyVo> childrenReplysPage = adminSpecialCommentReplyService.findAdminSpecialCommentReplys(adminSpecialCommentSearchPO1);
                record.setReplyNum(childrenReplysPage.getTotal());
                record.setChildren(childrenReplysPage);
            }
        }
        return adminSpecialVoPage;
    }

    /**
     * 保存和更新
     * @param adminSpecialCommentSavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public AdminSpecialCommentVo saveUpdateAdminSpecialComment(AdminSpecialCommentSavePO adminSpecialCommentSavePO){
        AdminSpecialComment adminSpecial = tranferBo(adminSpecialCommentSavePO, AdminSpecialComment.class);
        adminSpecial.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminSpecial.setSystemName(SystemIdThrealLocal.get().getSystemName());
        this.saveOrUpdate(adminSpecial);
        AdminSpecialCommentVo adminSpecialVo = tranferBo(adminSpecial, AdminSpecialCommentVo.class);
        return adminSpecialVo;
    }

    /**
     * 根据ID获取明细
     * @param id
     * @return
     */
    public AdminSpecialCommentVo getAdminSpecialCommentById(Long id){
        AdminSpecialComment adminSpecial = this.getById(id);
        AdminSpecialCommentVo adminSpecialVo = tranferBo(adminSpecial, AdminSpecialCommentVo.class);
        return adminSpecialVo;
    }

    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminSpecialCommentById(Long id){
        boolean b = this.removeById(id);
        return b;
    }


    /**
     * 批量物理删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRemovesAdminSpecialComment(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }


    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatus(Long id,String column,Integer value){
        boolean b = super.updateStatus(id,column,value);
        if(b){
            adminSpecialCommentReplyService.updateStatusByParentId(id,column,value);
        }
        return b;
    }

    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatusStr(Long id,String column,String value){
        boolean b = super.updateStatusStr(id,column,value);
        if(b){
            adminSpecialCommentReplyService.updateStatusByParentId(id,column,value);
        }
        return b;
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchDowns(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminSpecialComment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.UN_STATUS.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        if(b){
            adminSpecialCommentReplyService.batchUpdateStatusByParentIds(ids,"status", StatusEnums.UN_STATUS.getCode());
        }
        return b;
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchUps(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminSpecialComment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.STATUS.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        if(b){
            adminSpecialCommentReplyService.batchUpdateStatusByParentIds(ids,"status", StatusEnums.STATUS.getCode());
        }
        return b;
    }
}
